package com.sheyu.mall.order.interceptor;

import com.sheyu.common.constant.AuthServerConstant;
import com.sheyu.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author sheyu
 * @Desciption:
 * @date 2020/9/8 8:15 上午
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberResponseVo> loginuser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String uri = request.getRequestURI();
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        boolean matchOrder = antPathMatcher.match("/order/omsorder/status/**",uri);
        boolean matchPay = antPathMatcher.match("/payed/notify",uri);
        if (matchOrder||matchPay){
            return true;
        }
        MemberResponseVo attribute = (MemberResponseVo)request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);

        if (attribute!=null){
            loginuser.set(attribute);
            return true;
        }else{
            request.getSession().setAttribute("msg", "请先进行登录");
            response.sendRedirect("http://localhost:20000/login.html");
            return false;
        }
    }
}
